﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace JqueryNet.FormControl.FormHtmlControl
{
    public class JHtmlSelect : JHtmlControl
    {
        private System.Web.UI.HtmlControls.HtmlSelect htmlSelect;

        public JHtmlSelect(System.Web.UI.HtmlControls.HtmlSelect htmlSelect)
            : base(htmlSelect)
        {
            // TODO: Complete member initialization
            this.htmlSelect = htmlSelect;
        }

        public override void Attr(string key)
        {
            switch (key.ToLower())
            {
                case "index":
                case "selectindex":
                    Value = htmlSelect.SelectedIndex;
                    return;
                case "size":
                    Value = htmlSelect.Size;
                    return;
                case "multiple":
                    Value = htmlSelect.Multiple;
                    return;
                case "items":
                    Value = htmlSelect.Items;
                    return;
                case "source": Value = htmlSelect.DataSource; return;
                case "textfield": Value = htmlSelect.DataTextField; return;
                case "valuefield": Value = htmlSelect.DataValueField; return;
                default:
                    base.Attr(key);
                    return;
            }
        }

        public override void Attr(string key,string value)
        {
            int b;
            int.TryParse(value,out b);
            bool f;
            bool.TryParse(value, out f);
            switch (key.ToLower())
            {
                case "index":
                case "selectindex":
                    htmlSelect.SelectedIndex = b;
                    return;
                case "size":
                    htmlSelect.Size = b;
                    return;
                case "multiple":
                    htmlSelect.Multiple=f;
                    return; 
                default:
                    base.Attr(key);
                    return;
            }
        }

        public override void Html()
        {
            Value = htmlSelect.InnerHtml;
        }

        public override void Html(Func<string> fun)
        {
            htmlSelect.InnerHtml = fun();
        }

        public override void Html(string value)
        {
            htmlSelect.InnerHtml = value;
        }

        public override void Text()
        {
            Value = htmlSelect.InnerText;
        }

        public override void Text(Func<string> fun)
        {
            htmlSelect.InnerText = fun();
        }

        public override void Text(string value)
        {
            htmlSelect.InnerText = value;
        }

        public override void Val()
        {
            List<ListItem> list = new List<ListItem>();
            if (htmlSelect.Multiple)
            {
                foreach (ListItem item in htmlSelect.Items)
                {
                    if (item.Selected)
                    {
                        list.Add(item);
                    }
                }
                Value = list.ToArray();
            }
            else
            {
                Value = htmlSelect.Value;
            }            
        }

        public override void Val(string value)
        {
            htmlSelect.Value = value;
        }

        public override void Val(string[] values)
        {
            foreach (ListItem item in htmlSelect.Items)
            {
                if (values.Contains(item.Value))
                {
                    item.Selected = true;
                }
            }
        }

    }
}
